lv.require.config({
//	baseUrl: 'http://149.235.139.153:8080/WebViewer/library/js', // Base URL is /js anyway, that is inherited from the index.html, data-main attribute.
//	baseUrl: 'http://' + window.OTCVA.serverName + ':8080/WebViewer/library/js', // Base URL is /js anyway, that is inherited from the index.html, data-main attribute.
	paths: {
		jquery: 'lib/jquery',
		jqueryui: 'lib/jquery-ui',
		underscore: 'lib/underscore',
		backbone: 'lib/backbone',
		text: 'lib/text'
	},
	shim: {
		'underscore': {
			exports: '_'
		},
		'jquery': {
			exports: '$'
		},
		'jqueryui': {
			deps:['jquery']
		},
		'backbone': {
			deps: ['underscore','jquery'],
			exports: 'Backbone'
		}
	}
});

var appCTX = {};
var libraryApp;
var libraryRouter;

//require( ['underscore','jquery','backbone', '../model/book', '../view/bookview' ], function(_,$,Backbone, Book, BookView) {
// require( ['underscore', 'jquery', 'jqueryui' ], function( _, $) {
lv.require( ['underscore', 'jquery', 'backbone', 'view/appview', 'library_router', 'jqueryui' ], function( _, $, Backbone, LibraryApp, LibraryRouter ) {

	console.log("Application instanciated ...");

	function applicationRun() {
		// Run the main application ...
		appCTX = _.extend( appCTX, Backbone.Events );
		/* Global object for message pumping across the application.
		 * The pattern "ListenTo" allows to register events originating
		 * in other objects, which is a very useful pattern in this app.
		 */
	
		//$("#mainViewAccordion").accordion();
		$("a,button").button();
		$("#releaseDate").datepicker();
		/* This can be a possible place to put the UI widgets from jQuery UI.
		 * Don't really know if I want to put it here, the best way might
		 * be actually the View ...
		 */
	
	    //$("#menu").menu();
		 $("#add-book-form-id").button();
		 $("#show-library-id").button();
	
		libraryApp = new LibraryApp();
		libraryRouter = new LibraryRouter();

		Backbone.history.start();
		
		console.log("Application runs");
	};

	function myApp( docDescriptor ) {

		var tempNamespace = window.OTCVA || (window.OTCVA = {} );
		var script = window.document.getElementById('OTCVA.MAIN.APP'),
			 myself = window.document.getElementById('OTCVA.DOCUMENT'),
			 newjs = window.document.createElement('script');

		window.OTCVA.documentDescriptor = docDescriptor;
		
		// IE
		newjs.onreadystatechange = function () {
			if (newjs.readyState === 'loaded' || newjs.readyState === 'complete') {
				console.log("IE specific: Document loaded");
				applicationRun();
			}
		};
		
		// others
		newjs.onload = function () {
				console.log("Document loaded");
				applicationRun();
		};

		if( (typeof myself === "object" ) && (myself != null ) ) {
			console.log("Removing the old document");
			myself.parentElement.removeChild(myself);
		}
	
		if(( typeof window.OTCVA.serverName === "string" ) && ( window.OTCVA.serverName != null )) {
			newjs.src = 'http://' + window.OTCVA.serverName + ':8080/WebViewer/ctxdocument?url=' + window.OTCVA.documentDescriptor;
			newjs.setAttribute('id',"OTCVA.DOCUMENT");
			script.parentNode.insertBefore(newjs);
		} else {
			console.error("Error: something went wrong with initialization of the application");
		}
	}

	//slow down loading of the application to simulate complexity
	function sleep(milliseconds) {
		var start = new Date().getTime();
		for (var i = 0; i < 1e7; i++) {
			if ((new Date().getTime() - start) > milliseconds){
				break;
    		}
  		}
	}
	sleep(1000);
	
	window.OTCVA.showDocument=myApp;
	return window.OTCVA.run;
});
